package util;

import java.util.ArrayList;
import java.util.List;

import com.rORM.core.Query;
import com.rORM.core.QueryFactory;

import database.LessonTable;
import database.Score;
import database.Student;

public class GetStuInfo {
	
	
	public GetStuInfo(){
		
	}
	
	Query q = QueryFactory.createQuery();
	int cn;
	

	public int getCn() {
		return cn;
	}
	public void setCn(int cn) {
		this.cn = cn;
	}
	public List<Score> getScore(int id){
		List<Score> l = new ArrayList<Score>();
		l = q.queryRows("select * from score where stuid = ?", Score.class, new Object[]{id});
		return l;
	}
	public List<LessonTable> getLessonTable(int LessonID){
		List<LessonTable> lt = new ArrayList<LessonTable>();
 		lt = q.queryRows("sqlect * from LessonTable where lessonID = ?", LessonTable.class, new Object[]{LessonID});
 		return lt;
	}
	public LessonTable GetCourseName(int CourseNum){
		LessonTable lt = new LessonTable();
		lt = (LessonTable)q.queryUniqueRow("select * from LessonTable where lessonID = ?", LessonTable.class, new Object[]{CourseNum});
		return lt;
	}
	
	public Object[] getUniqueRowStuInfo (Student s) {
        List<Score> scores = getScore(s.getId());
        List<Integer> score = new ArrayList<Integer>();
        List<String> courseName = new ArrayList<String>();
        int totalLessonScore = 0;
        int totalScore = 0;
        int cn = 0;
        if (scores == null) {
            return new Object[]{
                    s.getName(), s.getId(),
                    null, null,
                    null, null,
                    null, null,
                    null, null,
                    null, null,
            };
        }
            for (Score s1 : scores) {
                score.add(s1.getScore());
                courseName.add(GetCourseName(s1.getCourseNum()).getLessonName());
                totalLessonScore += GetCourseName(s1.getCourseNum()).getEachScore();
                totalScore += s1.getScore();
            }
            cn = scores.size();
            this.cn = cn;
            if (cn == 5) {
                return new Object[]{s.getName(), s.getId(),
                        courseName.get(0), score.get(0),
                        courseName.get(1), score.get(1),
                        courseName.get(2), score.get(2),
                        courseName.get(3), score.get(3),
                        courseName.get(4), score.get(4),
                        totalLessonScore, totalScore,
                };
            } else if (cn == 4) {
                return new Object[]{s.getName(), s.getId(),
                        courseName.get(0), score.get(0),
                        courseName.get(1), score.get(1),
                        courseName.get(2), score.get(2),
                        courseName.get(3), score.get(3),
                        null, null,
                        totalLessonScore, totalScore,
                };
            } else if (cn == 3) {
                return new Object[]{s.getName(), s.getId(),
                        courseName.get(0), score.get(0),
                        courseName.get(1), score.get(1),
                        courseName.get(2), score.get(2),
                        null, null,
                        null, null,
                        totalLessonScore, totalScore,
                };
            } else if (cn == 2) {
                return new Object[]{s.getName(), s.getId(),
                        courseName.get(0), score.get(0),
                        courseName.get(1), score.get(1),
                        null, null,
                        null, null,
                        null, null,
                        totalLessonScore, totalScore,
                };
            } else if (cn == 1) {
                return new Object[]{s.getName(), s.getId(),
                        courseName.get(0), score.get(0),
                        null, null,
                        null, null,
                        null, null,
                        null, null,
                        totalLessonScore, totalScore,
                };
            } else {
                return null;
            }
    }





	public static void main(String args[]){
		Query q = QueryFactory.createQuery();
		Student s = (Student)q.queryUniqueRow("select * from student where id = ?", Student.class, new Object[]{16402});
		new GetStuInfo().getUniqueRowStuInfo(s);
	}

}
